const express = require('express');
const jwts = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const db = require('./db');
const config = require('./config.json');

const core = require('@huaweicloud/huaweicloud-sdk-core');
const iotda = require("@huaweicloud/huaweicloud-sdk-iotda/v5/public-api");
const token = require('./token.json');

const router = express.Router();

const ak = token.ak;
const sk = token.sk;
const endpoint = "https://iotda.cn-north-4.myhuaweicloud.com";
const project_id = "7ca5090c9ecb43f4a3ee138400ed98df";

let datas = "";


const credentials = new core.BasicCredentials()
  .withAk(ak)
  .withSk(sk)
  .withProjectId(project_id)
const client = iotda.IoTDAClient.newBuilder()
  .withCredential(credentials)
  .withEndpoint(endpoint)
  .build();

function fetchData() {
  const request = new iotda.ShowDeviceShadowRequest();
  request.deviceId = "660974affb8177243a534265_hi3861";
  const result = client.showDeviceShadow(request);
  result.then(result => {
    datas = result.shadow[0].reported.properties;
    console.log(datas);
    //console.log("tem: " + JSON.stringify(result.shadow[0].reported.properties.tem));
  }).catch(ex => {
    console.log("exception:" + JSON.stringify(ex));
  });
}
setInterval(fetchData, 10000);

router.post('/register', async (req, res) => {
  const { username, password } = req.body;
  if (!username || !password) {
    return res.status(400).json({ message: '用户名和密码不能为空' });
  }

  // 检查用户名是否已存在
  const user = await db.getUser(username);
  if (user) {
    return res.status(400).json({ message: '用户名已存在' });
  }

  // 创建用户
  const salt = await bcrypt.genSalt(6);
  const hash = await bcrypt.hash(password, salt);
  await db.createUser(username, hash, null, null);

  console.log('\x1b[32m%s\x1b[0m users “' + username + '” Registed', '[INFO]:');

  res.json({ message: '注册成功' });
});

// 登录
router.post('/login', async (req, res) => {
  const { username, password } = req.body;
  if (!username || !password) {
    return res.status(400).json({ message: '用户名和密码不能为空' });
  }

  // 检查用户名和密码是否匹配
  const user = await db.getUser(username);
  if (!user || !await bcrypt.compare(password, user.password)) {

    console.log('\x1b[33m%s\x1b[0m users “' + username + '” Logining but error', '[WARN]:');

    return res.status(400).json({ message: '用户名或密码错误' });
  }

  function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  await sleep(1000); // 休眠 2 秒

  // 生成jwt
  const payload = { username: user.username, id: user.id, role: user.role };
  const token = jwts.sign(payload, config.jwtSecret, { expiresIn: '6h' });

  console.log('\x1b[32m%s\x1b[0m users “' + username + '” Logined' + ' With role: ' + user.role, '[INFO]:');

  console.log(datas);

  res.json({ token });
});

router.post('/get_data', async (req, res) => {
  const { jwt } = req.body;
  const token = jwt.replace(/^"(.*)"$/, '$1');

  jwts.verify(token, config.jwtSecret, (err, decoded) => {
    if (err) {
      return res.status(500).json({ message: 'jwt验证失败，请重新登陆' });
    }
  });
  res.json(datas);
});

router.post('/led', async (req, res) => {
  const { sta } = req.body;
  const request = new iotda.CreateCommandRequest();
  request.deviceId = "660974affb8177243a534265_hi3861";
  request.instanceId = "f2cf8829-804b-4039-9736-bc8ac7e3f5a0";

  if (sta == 0) {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"OFF\"}");
    body.withCommandName("led");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  } else {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"ON\"}");
    body.withCommandName("led");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  }
});

router.post('/fan', async (req, res) => {
  const { sta } = req.body;
  const request = new iotda.CreateCommandRequest();
  request.deviceId = "660974affb8177243a534265_hi3861";
  request.instanceId = "f2cf8829-804b-4039-9736-bc8ac7e3f5a0";

  if (sta == 0) {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"OFF\"}");
    body.withCommandName("fan");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  } else {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"ON\"}");
    body.withCommandName("fan");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  }
});

router.post('/sig', async (req, res) => {
  const { sta } = req.body;
  const request = new iotda.CreateCommandRequest();
  request.deviceId = "660974affb8177243a534265_hi3861";
  request.instanceId = "f2cf8829-804b-4039-9736-bc8ac7e3f5a0";

  if (sta == 0) {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"OFF\"}");
    body.withCommandName("sig");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  } else {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"ON\"}");
    body.withCommandName("sig");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  }
});

router.post('/sg', async (req, res) => {
  const { sta } = req.body;
  const request = new iotda.CreateCommandRequest();
  request.deviceId = "660974affb8177243a534265_hi3861";
  request.instanceId = "f2cf8829-804b-4039-9736-bc8ac7e3f5a0";

  if (sta == 0) {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"OFF\"}");
    body.withCommandName("sg");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  } else {
    const body = new iotda.DeviceCommandRequest();
    body.withParas("{\"value\":\"ON\"}");
    body.withCommandName("sg");
    body.withServiceId("attribute");
    request.withBody(body);
    const result = client.createCommand(request);
    result.then(result => {
    }).catch(ex => {
      console.log("exception:" + JSON.stringify(ex));
    });
  }
});

module.exports = router;

